package com.tubi.edu.info.manager.service;

import com.tubi.edu.info.manager.dao.BaseStudentDao;
import com.tubi.edu.info.manager.dao.OtherStudentDao;
import com.tubi.edu.info.manager.dao.StudentDao;
import com.tubi.edu.info.manager.domain.Student;
import com.tubi.edu.info.manager.factory.StydentDaoFactory;

//业务员
public class StudentService {
    //1。创建库管类
    //private OtherStudentDao StudentDao = new OtherStudentDao();

    //这里是通过学生库管工厂类，获取库管对象  （多态）
    private BaseStudentDao StudentDao =  StydentDaoFactory.getStudentDao();

    //添加学生
    public boolean addStudent(Student stu) {
        //2。将学生对象，传递给库管中德addstudent方法
        boolean result = StudentDao.addStudent(stu);
        //3。将返回布尔类型的结果，返回客服
        return result;
    }
    //判断学号是否存在
    public boolean isEcists(String id) {
        Student[]  stus =  StudentDao.findAllStudent(); //返回对象的学生数组
        boolean exists = false;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            //判断学号是否存在
            if(stu!=null && id.equals(stu.getId())){
                exists =  true;
                break;
            }
        }
        return exists;
    }
    //查看学生
    public Student[] findAllStudebt() {
        Student[]  stus =  StudentDao.findAllStudent(); //返回对象的学生数组
        //2.判断是否有学生信息
        boolean flag = false;
        for (int i = 0; i < stus.length; i++) {
            if(stus[i] != null){
                flag = true;
            }
        }
        if(flag) {
            return stus;
        }else {
            return null;
        }

    }
    //删除学生
    public void deleteStudentById(String id) {
        StudentDao.deleteStudentById(id);
    }
    //修改学生
    public void updateStudent(String id, Student stu) {
        StudentDao.updateStudent(id,stu);
    }
}
